package com.huikeportal.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.controller.common.Lang;
import com.huikeportal.controller.common.MessageManager;
import com.huikeportal.model.AspnetUsers;
import com.huikeportal.model.Language;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteDataManagerInfo;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.service.CommonService;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.SiteService;

/**
 * 店铺基础数据管理
 * 
 * @author focus
 * 
 */
@Controller
public class ShopAPI {

	@Autowired
	private SiteService siteService;
	
	@Autowired
	private InterFaceCommon interFaceCommon;
	
	@Autowired
	private CommonService commonService;
	

	/**
	 * 获取店铺信息
	 * 
	 * @param square 广场编码
	 * @param floor 楼层编码
	 * @param siteName 店铺名称
	 * @param userId 用户ID
	 * @param currentPage 查找的页数
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_getShopInfos", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getShopInfos(String square,
			String floor,String siteName,String userId, String currentPage) throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getShopInfos");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		
		//1、是否为空验证、参数合法性验证
		//userId验证
		StringBuffer reMessage = new StringBuffer();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		if(userId == null || "".equals(userId)){
			reMessage.append("userId为空错误;");
		}else{
			try {
				AspnetUsers aspnetUsers = commonService.getaspnetUsersMapper(userInfo);
				if(aspnetUsers == null){
					reMessage.append("userId不合法;");
				}else{
					userInfo.setUserName(aspnetUsers.getUsername());
				}
			} catch (Exception e) {
				reMessage.append("userId不合法;");
			}
		}
		
		if(square == null || "".equals(square)){
			reMessage.append("square为空错误");
		}
		
		if(floor == null || "".equals(floor)){
			reMessage.append("floor为空错误");
		}
		
		if(currentPage == null || "".equals(currentPage)){
			reMessage.append("currentPage为空错误");
		}
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			SiteDataManagerInfo siteDataManagerInfo = new SiteDataManagerInfo();
			
			String permitSitesStr = getAllPermitSitesStr(userInfo);
			siteDataManagerInfo.setSiteArea(permitSitesStr);
			
			siteDataManagerInfo.setSquare(square);
			siteDataManagerInfo.setFloor(floor);
			siteDataManagerInfo.setSiteName(URLDecoder.decode(siteName, "UTF-8"));
			siteDataManagerInfo.setSource(MessageManager.getMsg("selectSource"));
			
			PageDataVo pageDataVo = new PageDataVo();
			// 默认显示第一页
			if (currentPage == null || "".equals(currentPage)) {
				pageDataVo.setCurrentPage(1);
			} else {
				pageDataVo.setCurrentPage(Integer.parseInt(currentPage));
			}
			// 获取每页显示条数
			pageDataVo.setPageSize(Integer.parseInt(MessageManager
					.getMsg("pageSize")));
			// 设置开始显示条数
			pageDataVo.setStartRow((pageDataVo.getCurrentPage() - 1)
					* pageDataVo.getPageSize());
			// 设置参数
			pageDataVo.setSearchParameter(siteDataManagerInfo);
			// 获取当前页用户信息
			pageDataVo = siteService.getSiteDataList(pageDataVo);
			
			map.put("siteList", pageDataVo.getPageResult());// 结果集
			map.put("currentPage", pageDataVo.getCurrentPage());// 当前页
			map.put("maxPage", pageDataVo.getMaxPage());// 最大页
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(map);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
	}
	
	
	/**
	 * 查询店铺明细
	 * 
	 * @param siteId 店铺编码
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_getShopDetailInfo", produces = { "application/json;charset=UTF-8" })
	public String huike_getShopDetailInfo(String siteId)
			throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getShopDetailInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(siteId == null || "".equals(siteId)){
			reMessage.append("参数siteId为空错误!");
		}
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			SiteDataManagerInfo siteDataManagerInfo = siteService.queryBySiteId(siteId);
			map.put("siteDetailInfo", siteDataManagerInfo);
			
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(map);
			
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	
	/**
	 * 修改店铺基础数据
	 * 
	 * @param siteId 店铺编码
	 * @param netArea 店铺面积
	 * @param staffNum 店员人数
	 * @return String 接口调用情况
	 */
	@RequestMapping(value = "/huike_updateShopInfo", produces = { "application/json;charset=UTF-8" })
	public String updateSiteData(String siteId,String netArea,String staffNum)
			throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_updateShopInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		// 校验参数
		//1、是否为空验证、参数合法性验证
		StringBuffer reMessage = new StringBuffer();
		if(siteId == null || "".equals(siteId)){
			reMessage.append("参数siteId为空错误!");
		}
		if(netArea == null || "".equals(netArea)){
			reMessage.append("参数netArea为空错误!");
		}
		if(staffNum == null || "".equals(staffNum)){
			reMessage.append("参数staffNum为空错误");
		}
		
		//如果验证有错误返回错误信息
		if(reMessage.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMessage.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		
		try{
			SiteDataManagerInfo info = new SiteDataManagerInfo();
			info.setSiteId(siteId);
			info.setNetArea(Float.parseFloat(netArea));
			info.setStaffNo(Short.parseShort(staffNum));
			MessageCommon messageCommon = siteService.updateSiteData(info);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch(Exception e){

			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		
		}
	}
	
	
	
	
	
	// 获取该用户所拥有的所有的店铺权限
	private String getAllPermitSitesStr(UserInfo userInfo) throws Exception{
		String permitSitesStr = "";
		
		// lowPermission < 300 的是广场以上的权限
		List<SiteAllInfo> permitSiteList = siteService.getSitesByUser(userInfo);
		List<SiteAllInfo> allSiteList = new ArrayList<SiteAllInfo>();
		int lowPermission = siteService.getLowPermission(permitSiteList);
		if(lowPermission < 300){
			List<SiteAllInfo> allGcList = siteService.getAllGcList(permitSiteList);
			allSiteList = siteService.getAllSiteList(allGcList);
			allSiteList.addAll(allGcList);
		}else if(lowPermission > 500){
			allSiteList = permitSiteList;
		}else{
			allSiteList = siteService.getAllSiteList(permitSiteList);
		}
		permitSitesStr = siteService.getSitePermitAreaStr(allSiteList);
		return permitSitesStr;
	}
	
}
